package com.jinxiangyang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Course;
import com.jinxiangyang.domain.vo.CourseFormVo;
import com.jinxiangyang.domain.vo.CoursePublishVo;
import com.jinxiangyang.domain.vo.CourseQueryVo;
import com.jinxiangyang.service.CourseService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/course")
@Api(tags = "课程管理模块")
public class CourseController {

    @Value("${uploadFolder}")
    private String uploadPath;

    @Autowired
    private CourseService courseService;
    /**
     *
     * @param currentPage 当前页
     * @param limit  每页显示数量
     * @param courseQueryVo 分页条件查询条件
     * @return
     */
    @ApiOperation(value = "点播课程列表")
    @GetMapping("/getList/{currentPage}/{limit}")
    @OperateLog(title = "课程管理模块",businessType = BusinessType.OTHER,operatorType = OperatorType.TEACHER)
    public AjaxResult getList(@PathVariable Long currentPage, @PathVariable Long limit,
                              CourseQueryVo courseQueryVo) {
        Page<Course> pageParam = new Page<>(currentPage, limit);
       Map<String,Object> map= courseService.findPageCourse(pageParam, courseQueryVo);
        return AjaxResult.success(map);
    }


    /**
     * 添加课程的基本信息
     * @param courseFormVo
     * @return
     */
    @ApiOperation("添加课程基本信息")
    @PostMapping("/save")
    @OperateLog(title = "课程管理模块",businessType = BusinessType.INSERT,operatorType = OperatorType.TEACHER)
    public AjaxResult save(@RequestBody CourseFormVo courseFormVo){
       Long courseId = courseService.saveCourseInfo(courseFormVo);
       return AjaxResult.success(courseId);
    }


    @ApiOperation("根据id查询课程")
    @GetMapping("/get/{id}")
    @OperateLog(title = "课程管理模块",businessType = BusinessType.OTHER,operatorType = OperatorType.TEACHER)
    public AjaxResult getCourseById(@PathVariable Long id){
        CourseFormVo courseFormVo =   courseService.getCourseById(id);
        return AjaxResult.success(courseFormVo);
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/update")
    @OperateLog(title = "课程管理模块",businessType = BusinessType.UPDATE,operatorType = OperatorType.TEACHER)
    public AjaxResult update(@RequestBody CourseFormVo courseFormVo){
        Long courseId =  courseService.updateCourseById(courseFormVo);
        return AjaxResult.success(courseId);
    }


    @ApiOperation("课程图片上传")
    @PostMapping("/file")
    @OperateLog(title = "课程管理模块",businessType = BusinessType.UPDATE,operatorType = OperatorType.TEACHER)
    public AjaxResult file(@RequestParam(value = "file",required = true) MultipartFile file){

        System.out.println("uploadPath = " + uploadPath);
        return null;

    }


    @ApiOperation("根据课程id获取课程信息")
    @GetMapping("/getCoursePublishVo/{id}")
    public AjaxResult getCoursePublishVoById(@PathVariable Long id){
       CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
       return AjaxResult.success(coursePublishVo);
    }


    @ApiOperation("课程发布接口")
    @PutMapping("publishCourse/{id}")
    public AjaxResult publishCourse(@PathVariable Long id){
        courseService.publishCourse(id);
        return AjaxResult.success();
    }


    @ApiOperation("删除课程")
    @DeleteMapping("remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        courseService.removeCourseId(id);
        return AjaxResult.success();
    }

}

